﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using TJADRICD.Model;
using TJADRICD.DLL;
namespace TJADRICD.CollaborativeDesignPlugin
{
    public class AccountDao
    {
        public bool UserIdPwdConfirm(String userID, String userPwd)
        {
            bool isAccountExist = false;
            string selectStr = "select * from account where User_ID = '" + userID + "' and Password = '" + userPwd + "'";
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    isAccountExist = true;
                }
                conn.Close();
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);
                return isAccountExist;
            }
            return isAccountExist;
        }
        public String getUserIdByUserName(String userName)
        {
            String userId = null;
            String signFileName = TJADRICD.DLL.CONST.vrfFilePath + userName + ".vrf";
            string selectStr = "select * from account where Sign_file = '" + signFileName + "'";
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    userId = reader[0].ToString();
                }
                conn.Close();
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);
                return userId;
            }
            return userId;
        }
        public String getUserNameById(String userID)
        { 
            String userName = null;
            String signFile = null;
            string selectStr = "select * from account where User_ID = '" + userID + "'";
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    signFile = reader[3].ToString();
                }
                conn.Close();
                reader.Close();
                //signFile = "C:\\TJADRICD\\Sign\\陈志东.vrf";
                string[] fileNameList = signFile.Split('\\');
                String fileName = fileNameList[fileNameList.Count<string>() - 1];
                string[] signNameList = fileName.Split('.');
                userName = signNameList[0];
            }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);
                return userName;
            }
            return userName;
        }
        public Account getAccountById(String userID)
        {
            string selectStr = "select * from account where User_ID = '" + userID + "'";
            Account account = null;
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    account = new Account();
                    account.UserId = reader[0].ToString();
                    account.Password = reader[1].ToString();
                    account.AuthPassword = reader[2].ToString();
                    account.Signfile = reader[3].ToString();
                }
                conn.Close();
                reader.Close();
             }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);
                return account;
            }
            return account;
        }

        public bool validatePwd(String userId, String pwd)
        {
            
            Account account = getAccountById(userId);
            if (account != null && account.Password.Equals(pwd))
            {
                return true;
            }
            return false;
        }

        public bool validateAuthPwd(String userId, String pwd)
        {

            Account account = getAccountById(userId);
            if (account != null && account.AuthPassword.Equals(pwd))
            {
                return true;
            }
            return false;
        }

        public void updateAccount(Account account)
        {
            string selectStr = "update  account set Password= '" + account.Password + "' , Auth_Password = '" + account.AuthPassword + "' where User_ID = '"+ account.UserId +"'";
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);
            }
        }
    }
}
